//
//  WPPDFView.m
//  PDF分页展示
//
//  Created by GZCP1897 on 17/1/16.
//  Copyright © 2017年 GZCP1897. All rights reserved.
//

#import "WPPDFView.h"

@implementation WPPDFView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext(); // 获取当前的绘图上下文
    [[UIColor whiteColor] set];
//
//    CGContextFillRect(context, rect);
//    CGContextGetCTM(context);
//    CGContextScaleCTM(context, 1, -1);
//    CGContextTranslateCTM(context, 0, -rect.size.height);
//
//
//    CGRect mediaRect = CGPDFPageGetBoxRect(self.pdfPageRef, kCGPDFCropBox);
//    CGContextScaleCTM(context, rect.size.width / mediaRect.size.width, rect.size.height / mediaRect.size.height);
//
//    /*进行坐标转换向右移动100个单位，并且向下移动当前视图得高度，
//
//     这是因为Quartz画图得坐标系统是以左下角为开始点，但iphone视图是以左上角为开始点
//
//     */
//    CGContextTranslateCTM(context, -mediaRect.origin.x, -mediaRect.origin.y);
//
//    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
//    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
//    CGContextDrawPDFPage(context, self.pdfPageRef); // 绘制当前页面
    
    NSString *fileName = @"example_pdf";//Book1  example_pdf
    NSString * pdfPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
    
    int tmp = arc4random() %22 + 1;
    
    MyDisplayPDFPage(context, tmp, pdfPath.UTF8String, rect);
}

CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename)
{
    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    size_t count;
    
    path = CFStringCreateWithCString (NULL, filename,
                                      kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, // 1
                                         kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    document = CGPDFDocumentCreateWithURL (url);// 2
    CFRelease(url);
    count = CGPDFDocumentGetNumberOfPages (document);// 3
    if (count == 0) {
        printf("`%s' needs at least one page!", filename);
        return NULL;
    }
    return document;
}
void MyDisplayPDFPage (CGContextRef myContext,
                       size_t pageNumber,
                       const char *filename, CGRect rect)
{
    CGPDFDocumentRef document;
    CGPDFPageRef page;
    
    document = MyGetPDFDocumentRef (filename);// 1
    page = CGPDFDocumentGetPage (document, pageNumber);// 2
    
    CGContextFillRect(myContext, rect);
    CGContextGetCTM(myContext);
    CGContextScaleCTM(myContext, 1, -1);
    CGContextTranslateCTM(myContext, 0, -rect.size.height);
    CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    CGContextScaleCTM(myContext, rect.size.width / mediaRect.size.width, rect.size.height / mediaRect.size.height);
    
        /*进行坐标转换向右移动100个单位，并且向下移动当前视图得高度，
    
         这是因为Quartz画图得坐标系统是以左下角为开始点，但iphone视图是以左上角为开始点
    
         */
    CGContextTranslateCTM(myContext, -mediaRect.origin.x, -mediaRect.origin.y);
    
    CGContextSetInterpolationQuality(myContext, kCGInterpolationHigh);
    CGContextSetRenderingIntent(myContext, kCGRenderingIntentDefault);
    
    
    CGContextDrawPDFPage (myContext, page);// 3
    CGPDFDocumentRelease (document);// 4
}









- (instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)dealloc{
    
}

@end
